<?php
namespace app\app\controller\v1;
use app\app\model\PropModel;
use app\app\model\UsersModel;
use app\app\model\BlockModel;
use app\app\util\OnlineUtils;
use app\app\util\BlockUtils;
use app\app\util\PointUtils;
use think\Db;

class BlockController extends BaseController{

    /**
     * 闯关模式结算
     */
    public function blockFight(){
        $user_id = OnlineUtils::getUserIdBySid();
        //失败结算
        //心数-1
        $users = new UsersModel();
        $users->user_id = $user_id;
        $number = -1;
        $users->reduceChance($number);
        //保存闯关层数
        $block = new BlockModel();
        $block->user_id = $user_id;
        //取出最高层数
        $ceng = BlockUtils::getUserBlock($user_id);
        //当前层数大于历史最高层
        if($ceng < input('post.number')){
            $block->saveNumber(input('post.number'));
            //redis保存闯关层数
            BlockUtils::foundUserBlock($user_id,input('post.number'));
        }
        //redis每日答题数目
        $answer = BlockUtils::getAnswer($user_id);
        $today = date('Y-m-d')." 23:59:59";
        $today = strtotime($today);
        $now   = time();
        $overTime = $today - $now;
        $key = $answer + input('post.number');
        BlockUtils::setAnswer($user_id,$key,$overTime);
        //加积分
        $point = config('POINT')*input('post.number');
        $users->addPoint($point);
        //redis积分排行
        PointUtils::getUserPoint($user_id,$point);
        //计算答对题目数,给与称号
        $achieve1 = Db::name('user_achieve')->where('user_id','eq',$user_id)->where('achieve_id','eq',1)->find();
        $achieve2 = Db::name('user_achieve')->where('user_id','eq',$user_id)->where('achieve_id','eq',2)->find();
        $achieve3 = Db::name('user_achieve')->where('user_id','eq',$user_id)->where('achieve_id','eq',3)->find();
        if(input('post.number') >= 20 && input('post.number') < 40){
            if(!$achieve1 && !$achieve2 && !$achieve3){
                Db::name('user_achieve')->insert(array('user_id'=>$user_id,'achieve_id'=>1,'create_time'=>time()));
                $achieve = Db::name('achieve')->where('id','eq',1)->find();
            }else{
                $achieve = '';
            }
        }elseif(input('post.number') >= 40 && input('post.number') < 80){
            if(!$achieve2 && !$achieve3){
                Db::name('user_achieve')->insert(array('user_id'=>$user_id,'achieve_id'=>2,'create_time'=>time()));
                $achieve = Db::name('achieve')->where('id','eq',2)->find();
            }else{
                $achieve = '';
            }
        }elseif(input('post.number') >= 80){
            if(!$achieve3){
                Db::name('user_achieve')->insert(array('user_id'=>$user_id,'achieve_id'=>3,'create_time'=>time()));
                $achieve = Db::name('achieve')->where('id','eq',3)->find();
            }else{
                $achieve = '';
            }
        }else{
            $achieve = '';
        }

        return jsonReturn(200,'success',['point'=>$point,'achieve'=>$achieve]);
    }



    /**
     * 使用复活卡
     */
    public function useRevive(){
        $user_id = OnlineUtils::getUserIdBySid();
        $prop    = new PropModel();
        $prop->user_id = $user_id;
        $result  = $prop->useProp();
        return jsonReturn($result['code'],$result['msg'],$result['data']);
    }

    /**
     * 闯关信息
     */
    public function blockInfo(){
        $user_id = OnlineUtils::getUserIdBySid();
        //取出排名
        $rank  = BlockUtils::getUserRank($user_id);
        $block = new BlockModel();
        $block->user_id = $user_id;
        //取出闯关数
        $data  = $block->getInfo();
        //取出心
        $users = new UsersModel();
        $user  = $users->getOne($user_id);
        //取出复活卡数目
        $prop  = new PropModel();
        $props = $prop->getUserProp($user_id);
        $result = array('rank'=>$rank+1,'num'=>$data['num'],'chance'=>$user['chance'],'revive'=>$props['num'],'share'=>$user['block_share']);
        return jsonReturn(200,'success',$result);
    }

    /**
     * 闯关模式分享
     */
    public function blockShare(){
        $user_id = OnlineUtils::getUserIdBySid();
        $users = new UsersModel();
        $users->user_id = $user_id;
        try{
            $users->getRevive();
        }catch (Exception $e){
            return jsonReturn(402,'分享失败');
        }
        //redis每日分享次数
        $share = BlockUtils::getShare($user_id);
        //更新数据
        $today = date('Y-m-d')." 23:59:59";
        $today = strtotime($today);
        $now   = time();
        $overTime = $today - $now;
        $value = $share + 1;
        BlockUtils::setShare($user_id,$value,$overTime);
        return jsonReturn(200,'success',$share);
    }

    /**
     * 闯关排行
     */
    public function blockRank(){
        $start = 0;
        $end   = 29;
        $list  = BlockUtils::getBlockRank($start,$end);
        $block = new BlockModel();
        //排行存在
        if($list){
            foreach($list as $k => $v){
                $user = $block->getOne($v);
                $data[] = $user;
            }
        }else{
            $data = array();
        }
        return jsonReturn(200,'success',$data);
    }

    /**
     *积分排行
     */
    public function pointRank(){
        $user_id = OnlineUtils::getUserIdBySid();
        $start = 0;
        $end   = 30;
        $data    = PointUtils::getPointRank($start,$end);
		// return jsonReturn(200,'success',$data);
        $users   = new UsersModel();
        if( $data ){
            foreach($data as $k => $v){
                $user = $users->getOne($v);
                $result['nickname'] = $user['nickname'];
                $result['points']   = $user['points'];
                $result['avatar_url'] = $user['avatar_url'];
				$result['id'] = $user['id'];
                $arr[]= $result;
            }
        }else{
            $arr = array();
        }
        //取出自己的排名
        $rank = PointUtils::getUserRank($user_id);
        return jsonReturn(200,'success',['rank'=>$arr,'my_rank'=>$rank+1]);
    }
}